﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Squog_1._1.Managers;

namespace Squog_1._1
{
    public class SquogSprite : PhysicsObject
    {
        //Abilities Vars
        int DNAFast = 0;
        int DNATeleport = 0;

        //Bouncing animation vars
        protected bool Bouncing;
        protected float BounceTheta;
        private Vector2 BounceOffset;

        //Throwing Vars
        private bool IsAiming;
        private Vector2 ThrowDir;
        private float ThrowPower;

        //Basic vars
        private Vector2 Center;
        private SpriteFont font;

        SquogProperties Data;

        //Scanner
        Scanner[] Scanner;

        ////Consumed when using ablities
        //private int Power;
        //private int MaxPower;
        //private int Health;
        //private int MaxHealth;
                    
        //#region AbilityAquired
        //private bool HasTeleport;
        //private bool HasSpeed;
        //#endregion
        //#region AbilityEquiped
        //private bool EquipedTeleport;
        //private bool EquipedSpeed;
        //#endregion


        public SquogSprite(SquogProperties Data)
            : base(Data.spriteSheet, 10)
        {
            this.Data = Data;
            IsAiming = false;

            //Add the annimations that will be used
            AddAnimationSequence("moving", 0, 4, 0, 1, 50, true);
            AddAnimationSequence("still", 0, 4, 0, 1, 50, true);

            //Set our starting animation
            ChangeAnimationSequence("still");

            Scale = 0.1f;
            Depth = 1;
            Bouncing = false;
            BounceTheta = 0;
            BounceOffset = new Vector2();
            font = DrawManager.Content.Load<SpriteFont>("GameFont");
            Center = new Vector2(105,35);
            Scanner = new Scanner[2];
            SpriteSheet s;
            Managers.SpriteSheet sd;
            Scanner[0] = new Scanner( DrawManager.GetSpriteSheet("scanner") );
            Scanner[0].InPlay = false;
            Scanner[1] = new Scanner( DrawManager.GetSpriteSheet("scanner") );
            Scanner[1].InPlay = false;
            PhysicsManager.Add(Scanner[0]);
            PhysicsManager.Add(Scanner[1]);
            DrawManager.Add(Scanner[0]);
            DrawManager.Add(Scanner[1]);
        }

        public override void Reset(Vector2 StartingPosition)
        {
            Scanner[0].Reset(StartingPosition);
            Scanner[1].Reset(StartingPosition);
            base.Reset(StartingPosition);
        }

        public override void Update(GameTime gameTime)
        {
            if (Data.Power < 100)
                Data.Power++;
            Move(InputManager.ThumbstickValue(Thumbsticks.Left,0));

            if (InputManager.IsPressed(ButtonControls.Jump, 0))
                Jump();

            if (InputManager.OnRelease(ButtonControls.Jump, 0))
                EndJump();

            if (InputManager.IsPressed(Thumbsticks.Right, 0))
                Aim();
            if (InputManager.OnRelease(Thumbsticks.Right, 0))
                IsAiming = false;

            if (InputManager.OnRelease(Triggers.Left, 0))
                Throw();

            AblilitiesUpdate();

            float Rate = 0.01f;
            int Height = 50;

            if (Bouncing)
                BounceTheta += gameTime.ElapsedGameTime.Milliseconds * Rate;
            else
                if (BounceTheta <= 0 || BounceTheta > MathHelper.Pi)
                    BounceTheta = 0;
                else
                    if (BounceTheta < MathHelper.PiOver2)
                        BounceTheta -= gameTime.ElapsedGameTime.Milliseconds * Rate;
                    else
                        BounceTheta += gameTime.ElapsedGameTime.Milliseconds * Rate;

            BounceTheta %= MathHelper.Pi;

            BounceOffset.Y = -(float)Math.Sin(BounceTheta % MathHelper.Pi) * Height;

            if (Velocity != Vector2.Zero)
            {
                ChangeAnimationSequence("moving");
            }
            else
            {
                ChangeAnimationSequence("still");
            }
        }

        public void AblilitiesUpdate()
        {
            if (Data.EquipedTeleport)
                TeleportAbilityUpdate();
            if (Data.EquipedSpeed)
                SpeedAbilityUpdate();
        }
        public void TeleportAbilityUpdate()
        {
            if (InputManager.OnPress(ButtonControls.Equiped1, 0))
                StartTeleport();

            if (InputManager.IsPressed(ButtonControls.Equiped1, 0))
            {
                if (Data.Power > 0)
                    Data.Power -= 6;
                else
                    EndTeleport();
                Teleport(InputManager.ThumbstickValue(Thumbsticks.Left, 0));
            }

            if (InputManager.OnRelease(ButtonControls.Equiped1, 0))
                EndTeleport();
        }

        public void SpeedAbilityUpdate()
        {
            if (InputManager.OnPress(ButtonControls.Equiped2, 0))
                StartSpeed();

            if (InputManager.IsPressed(ButtonControls.Equiped2, 0))
            {
                if (Data.Power > 0)
                    Data.Power -= 2;
                else
                    EndSpeed();
            }

            if (InputManager.OnRelease(ButtonControls.Equiped2, 0))
                EndSpeed();
        }

        public void Aim()
        {
            IsAiming = true;
            ThrowDir = InputManager.ThumbstickValue(Thumbsticks.Right, 0);
            ThrowDir.Y *= -1;
            ThrowPower = ThrowDir.Length();
            ThrowDir.Normalize();
        }
        public void Throw()
        {
            IsAiming = false;
            if (!Scanner[0].InPlay)
            {
                //Throw ball1
                Scanner[0].InPlay = true;
                Scanner[0].Position = Position;
                Scanner[0].Velocity = ThrowDir * ThrowPower * 40;
            }
            else if (!Scanner[1].InPlay)
            {
                //Throw ball2
                Scanner[1].InPlay = true;
                Scanner[1].Position = Position;
                Scanner[1].Velocity = ThrowDir * ThrowPower * 40;
            }
            else
            {
                //Scan between ball1 and ball2
                BallScan();
                Scanner[0].InPlay = false;
                Scanner[1].InPlay = false;
            }


        }

        public void BallScan()
        {
            Vector2 Direction = Scanner[1].Position - Scanner[0].Position;
            float Distance = Direction.Length();
            Direction.Normalize();

            if (Distance < 200)
            {
                //TODO: Animation
                DrawManager.Add(new ScanSprite(DrawManager.GetSpriteSheet(@"scan"), Scanner[0].Position,Scanner[1].Position));

                float Radius = Distance / 2;
                Vector2 MidPt = Scanner[0].Position + (Direction * Radius);
                List<PhysicsObject> Collisions = PhysicsManager.CollisionCheck(MidPt, Radius);
                foreach (PhysicsObject o in Collisions)
                    Scan(o);
            }
        }

        public void Scan(PhysicsObject Enemy)
        {
            //TODO: Animation
            if (Enemy is FastEnemy)
                DNAFast++;
            else if (Enemy is TeleportEnemy)
                DNATeleport++;
        }

        public override void PhysicsUpdate(GameTime gameTime)
        {
            base.PhysicsUpdate(gameTime);
        }

        public override void Draw(Vector2 ScreenPosition)
        {
            //Changed to use sprite effects to flip the
            //image.
            
            if (Velocity.X + ControlVelocity.X < 0)
                DrawEffects = SpriteEffects.None;
            else if (Velocity.X + ControlVelocity.X > 0)
                DrawEffects = SpriteEffects.FlipHorizontally;

            base.Draw(ScreenPosition + BounceOffset);

            DrawManaBox((float)Data.Power / Data.MaxPower);
            DrawHealthBox((float)Data.Health / Data.MaxHealth);
            if (IsAiming)
                DrawArrow(ScreenPosition, ThrowDir);
       }

        public void DrawManaBox(float Value)
        {
            DrawManager.spriteBatch.Draw(DrawManager.GetSpriteSheet(@"Sprites/dot").Texture, new Rectangle(550, 45, 240, 20), new Rectangle(0, 0, 1, 1), Color.White);
            DrawManager.spriteBatch.Draw(DrawManager.GetSpriteSheet(@"Sprites/dot").Texture, new Rectangle(552, 47, (int)(236 * Value), 16), new Rectangle(2, 0, 1, 2), Color.Blue);
        }
        public void DrawHealthBox(float Value)
        {
            DrawManager.spriteBatch.Draw(DrawManager.GetSpriteSheet(@"Sprites/dot").Texture, new Rectangle(550, 20, 240, 20), new Rectangle(0, 0, 1, 1), Color.White);
            DrawManager.spriteBatch.Draw(DrawManager.GetSpriteSheet(@"Sprites/dot").Texture, new Rectangle(552, 22, (int)(236 * Value), 16), new Rectangle(2, 0, 1, 2), Color.Green);
        }

        public void DrawArrow(Vector2 Location, Vector2 Direction)
        {
            DrawManager.spriteBatch.Draw(DrawManager.GetSpriteSheet(@"arrow").Texture, Location, new Rectangle(0,0,100,50), Color.White, MyMath.AngleOf(Direction), new Vector2(1,25), ThrowPower, SpriteEffects.None,1);
        }
    }
}
